import sys
from pathlib import Path
from app.gui.main_window import MainWindow
from app.utils.crypto_utils import validate_license_code, get_local_mac_address
from app.utils.logger import Logger
from creat_license import encrypt_license_info


def check_license():
    log_file = "application_log.txt"
    logger = Logger(log_file)

    mac_address = get_local_mac_address()
    logger.log(f"mac_address:{mac_address}")
    if not mac_address:
        logger.log("无法获取本地MAC地址，授权验证失败。")
        return False

    license_code = encrypt_license_info(mac_address, "2025-01-01", 180)
    if validate_license_code(license_code):
        logger.log("授权验证成功。")
        return True
    else:
        logger.log("授权验证失败。")
        return False


if __name__ == "__main__":
    # 添加项目根目录到系统路径
    BASE_DIR = Path(__file__).resolve().parent.parent
    sys.path.append(str(BASE_DIR))

    if check_license():
        app = MainWindow()
        app.mainloop()
    else:
        print("授权验证失败，程序终止。")